package main;

import bean.ConflictWorker;
import bean.Task;
import bean.TaskTeam;
import bean.Worker;
import util.DiversityUtil;
import util.DivideConquer2Util;
import util.GreedyUtil;

import java.util.ArrayList;

public class DivideConquer2 {

    public double[] dc(ArrayList<Worker> Wp, ArrayList<Task> Tp) {
        GreedyUtil gUtil = new GreedyUtil();
        DivideConquer2Util dcUtil = new DivideConquer2Util();

        int countYes = 0;
        int countNo = 0;
        double sumEntropy = 0.0;
        double avgEntropy = 0.0;
        double TpCost = 0.0;
        int workerNum = 0;//参与任务的人数量

        //算法开始
        ArrayList<TaskTeam> taskTeams = new ArrayList<>();
        ArrayList<TaskTeam> taskTeamsN = new ArrayList<>();

        //1. 首先为所有任务匹配最佳的团队
        for (Task tj : Tp) {
            //1.1 为每个任务tj找到有效工人tjWorkers
            ArrayList<Worker> tjWorkers = gUtil.getTjWorkers(tj,Wp);
            //1.2
            TaskTeam taskTeam = dcUtil.formTeam(tjWorkers, tj);

            if (taskTeam.isCompleted()) {
                taskTeams.add(taskTeam);
            }else {
                taskTeamsN.add(taskTeam);
            }
        }

        //2. 找到taskTeamsY中有冲突的工人及相应的任务
        ArrayList<ConflictWorker> conflictWorkers = dcUtil.findConflictWorkers(taskTeams);

        //3. 找到没有冲突的工人所对应的任务，确定这些工人并从Wp中移除
        ArrayList<TaskTeam> taskTeamsY = dcUtil.confirmWorker(Wp, conflictWorkers, taskTeams);

        //4. 为每个冲突的工人选择最佳的任务
        dcUtil.selectBetterTaskForWorker(conflictWorkers, taskTeams, taskTeamsY, taskTeamsN,Wp);










        double[] doubles = new double[5];
        doubles[0] = avgEntropy;
        doubles[1] = countYes;
        doubles[2] = countNo;
        doubles[3] = TpCost;
        doubles[4] = (double) workerNum;
        return doubles;
    }

}
